/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

function trackListingItemDisplay(){
      var width = $(window).width();
        if(width > 1280){
            var totalItem = parseInt(width/240);        
            var widthExpanded = totalItem * 240 + 20;
            var screenHolder = $("#js_controller_marketplace_index").find(".holder");
            screenHolder.css("width",widthExpanded + "px");    
            // Header Ajust
            $("#header .holder").css("width",widthExpanded + "px");
            $("#header_right").css("width",widthExpanded - 110 + "px");
            // Hide left panel
            //$("#navigator").addClass("toggle");
        }
        var $container = $("#marketplace_top_listing");
        $container.imagesLoaded(function (){
            $container.masonry({
                itemSelector: '.listing_item',
                columnWidth: 225,
                gutterWidth: 20
            });
        });  
}

$Behavior.marketplaceInit = function(){
    trackListingItemDisplay();
    $(window).resize(function() {
        trackListingItemDisplay();
    });
    
    // scrolling checking
    $(window).scroll(function() {
        if($(window).scrollTop() + $(window).height() > ($(document).height() - 100)) {
            var loading = $("#loading");
            if(loading.find('a').length > 0 && loading.find("#canvasLoader").length == 0){
                // Loader
                var cl = new CanvasLoader('loading');
                cl.setColor('#b3acb3');
                cl.setDiameter(65); // default is 40
                cl.setDensity(44); // default is 40
                cl.setRange(1); // default is 1.3
                cl.setSpeed(3); // default is 2
                cl.setFPS(21); // default is 24
                cl.show(); // Hidden by default

                // This bit is only for positioning - not necessary
                var loaderObj = document.getElementById("canvasLoader");
                loaderObj.style.position = "absolute";
                loaderObj.style["top"] = cl.getDiameter() * -0.5 + "px";
                loaderObj.style["left"] = cl.getDiameter() * -0.5 + "px";
            
                // Call load move
                var ajaxScript = loading.find('a');
                if(typeof ajaxScript != 'undefined'){
                    var script = ajaxScript.attr('onclick');
                    if(typeof script != 'undefined'){
                        script = script.replace('return false;','');
                        eval(script); 
                    }
                }
                else{
                    loading.remove();
                }                
            }
            
        }
    });
    
};
